#include "readsolomontest.h"
#include <newcode/ReedSolomonEncoder.h>
#include <zxing/common/reedsolomon/GF256.h>
#include <zxing/common/Array.h>

using namespace zxing;

ReadSolomonTest::ReadSolomonTest():
    rse(ReedSolomonEncoder(GF256::QR_CODE_FIELD)),
    rsd(ReedSolomonDecoder(GF256::QR_CODE_FIELD)){}

void ReadSolomonTest::testCase()
{
    QFETCH(QString, string);
    QFETCH(int, ecBytes);
    int total = string.size() + ecBytes;
    ArrayRef<int> toEncode(total);
    for(int i = 0; i < total; i++)
    {
        if (i < string.size())
        {
            toEncode[i] = string[i].toAscii() & 0xFF;
        } else
        {
            toEncode[i] = 0;
        }
    }

    rse.encode(toEncode, ecBytes);
    toEncode[1] = 0;
    toEncode[2] = 0;
    rsd.decode(toEncode, ecBytes);
    QString res(string.size());
    for (int i = 0; i < string.size(); i++)
    {
        res[i] = (QChar)toEncode[i];
    }

    QCOMPARE(res, string);
}

void ReadSolomonTest::testCase_data()
{
    QTest::addColumn<QString>("string");
    QTest::addColumn<int>("ecBytes");
    QTest::newRow("test1") << "test1" << 4;
    QTest::newRow("test2") << "long string" << 6;
}
